package com.xjwlmq.exam20241011;

import com.xjwlmq.dx.util.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName DictTypeDaoImpl
 * @Description TODO:
 * @Author 12994
 * @Date 2024/10/11 20:28
 * @Version 1.0
 **/
public class DictTypeDaoImpl extends BaseDao implements DictTypeBaseDao {
    @Override
    public ArrayList<DictType> getDictTypes() {
        ArrayList<DictType> dictTypes = new ArrayList<>();
        DictType thisDictType = null;
        try {
            this.rs = baseQuery("SELECT * FROM sys_dict_type;");
            if (this.rs == null) {
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisDictType = new DictType();
                thisDictType.setDictId(this.rs.getInt("dict_id"));
                thisDictType.setDictName(this.rs.getString("dict_name"));
                thisDictType.setDictType(this.rs.getString("dict_type"));
                thisDictType.setStatus(this.rs.getString("status"));
                thisDictType.setCreateBy(this.rs.getInt("create_by"));
                thisDictType.setUpdateBy(this.rs.getInt("update_by"));
                thisDictType.setRemark(this.rs.getString("remark"));
                dictTypes.add(thisDictType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dictTypes;
    }

    @Override
    public int addDictType(DictType dt) {
        return baseUpdate("INSERT INTO sys_dict_type (dict_id, dict_name, " +
                        "dict_type, status, create_by, update_by, remark) VALUES " +
                        "(null,?,?,?,?,?,?);",
                dt.getDictName(),
                dt.getDictType(),
                dt.getStatus(),
                dt.getCreateBy(),
                dt.getUpdateBy(),
                dt.getRemark());
    }

    @Override
    public int updateDictTypeViaDictId(DictType dt) {
        return baseUpdate("UPDATE sys_dict_type SET dict_name=?, " +
                        "dict_type=?, status=?, create_by=?, update_by=?, remark=? " +
                        "where dict_id=?;",
                dt.getDictName(),
                dt.getDictType(),
                dt.getStatus(),
                dt.getCreateBy(),
                dt.getUpdateBy(),
                dt.getRemark(),
                dt.getDictId());
    }

    @Override
    public int deleteDictTypeViaDictId(String dictId) {
        return baseUpdate("DELETE FROM sys_dict_type WHERE dict_id=?", dictId);
    }

    public ArrayList<DictType> getDictTypes(int dictId) {
        ArrayList<DictType> dictTypes = new ArrayList<>();
        DictType thisDictType = null;
        try {
            this.rs = baseQuery("SELECT * FROM sys_dict_type WHERE dict_id=?;", dictId);
            if (this.rs == null) {
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisDictType = new DictType();
                thisDictType.setDictId(this.rs.getInt("dict_id"));
                thisDictType.setDictName(this.rs.getString("dict_name"));
                thisDictType.setDictType(this.rs.getString("dict_type"));
                thisDictType.setStatus(this.rs.getString("status"));
                thisDictType.setCreateBy(this.rs.getInt("create_by"));
                thisDictType.setUpdateBy(this.rs.getInt("update_by"));
                thisDictType.setRemark(this.rs.getString("remark"));
                dictTypes.add(thisDictType);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return dictTypes;
    }
}
